package com.rs2hd.packethandler;

import com.rs2hd.model.NPC;
import com.rs2hd.model.Skills;
import com.rs2hd.model.World;
import com.rs2hd.net.Packet;
import org.apache.mina.common.IoSession;
import com.rs2hd.util.Misc;
import com.rs2hd.util.BanManager;
import com.rs2hd.event.Event;
import com.rs2hd.model.PlayerDetails;
import com.rs2hd.model.World;
import com.rs2hd.model.Player;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.InetSocketAddress;
import java.util.HashSet;
import java.util.Set;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import com.rs2hd.io.XStreamPlayerLoader;

/**
 * Handles any commands sent to the client.
 *
 * @author Graham
 * @editor Emery(ItsEmerica)
 */
public class CommandScreenHandler implements PacketHandler {


    private Set<String> bannedNames = new HashSet<String>();
    private Set<String> bannedAddresses = new HashSet<String>();
    private Set<String> muted = new HashSet<String>();
    
    private String[] getPlayerNames() {
    	int i = 0;
    	int pOnline = getPlayerCount();
    	String[] playersOnline = new String[500];
    	for(Player p : World.getInstance().getPlayerList()) {
			if (p != null) {
				playersOnline[i] = Misc.capitalize(p.getUsername());
				i++;
			}
		}
		return playersOnline;
    }
    
	private int getPlayerCount() {
		int playerCount = 0; 

		for(Player p : World.getInstance().getPlayerList()) {
			if (p != null) {
				playerCount++;
			}
		}
		return playerCount;
	}
	public void handlePacket(final Player player, IoSession session, Packet packet) {
		packet.readByte();
		String command = packet.readRS2String();
		String[] cmd = command.split(" ");
		cmd[0] = cmd[0].toLowerCase();
		try {
			/* -1 Donator
			 * 0 Regular
			 * 1 Silver
			 * 2 Gold - Etc
			 * 3 -----
			 * 4 Hidde
			*/
			if(player.getRights() >= -1) {
				if(cmd[0].equals("torm")) {
				    player.tele(3222 ,9618 , 0);
                    player.getActionSender().sendMessage("You Teleported to The Tormented Demon!.");
				}
				if(cmd[0].equals("corp")) {
				    player.tele(2966 ,4383 , 0);
                    player.getActionSender().sendMessage("You Teleported to The Corporeal Beast!.");
				}
				if(cmd[0].equals("edge")) {
				    player.tele(3093 ,3493 , 0);
                    player.getActionSender().sendMessage("You Teleported to Edgeville.");
				}
				if(cmd[0].equals("train")) {
				    player.tele(3172 ,2984 , 0);
                    player.getActionSender().sendMessage("You Teleported to Training Area.");
				}
				if (cmd[0].equals("begintrain")) {
					player.tele(3208 , 3203, 0);
                    player.getActionSender().sendMessage("Begin your training!");
				}
				if (cmd[0].equals("kdr")) {
					double KDR = ((double)player.KC)/((double)player.DC);
					player.forceChat("My Kill/Death ratio is "+player.KC+"/"+player.DC+"; "+KDR+".");
					World.getInstance().registerEvent(new Event(400) {
						@Override
						public void execute() {
							player.getUpdateFlags().setForceTextUpdateRequired(false);
							this.stop();
						}
					});
				}
				if(cmd[0].equals("pkbox")) {
                    player.tele(2913, 4449, 0);
                    player.getActionSender().sendMessage("You Teleported to Pking Box!.");
				}
				if(cmd[0].equals("home")) {
				    player.tele(2442, 3090, 0);
                    player.getActionSender().sendMessage("You Teleported to Home.");
				}
				if(cmd[0].equals("points")) {
			        player.getUpdateFlags().setForceTextUpdateRequired(true);
                    player.forceChat("I Have " + player.PkPoints + " Shop Points!");
				    World.getInstance().registerEvent(new Event(400) {
                        @Override
                        public void execute() {
                            player.getUpdateFlags().setForceTextUpdateRequired(false);
                            this.stop();
                        }
                    });
                }
                if(cmd[0].equals("multipk")) {
				    player.tele(1663 ,5696 , 0);
                    player.getActionSender().sendMessage("You Teleported to Multi Zone.");
				}
				if(cmd[0].equals("buy")) {
					player.getActionSender().sendMessage("<col=ff0000>The PK Shop glitch is fixed!");
					player.shop(player);
				}
				if(cmd[0].equals("yell")) {
					String yellText = command.substring(5);
					for(Player p : World.getInstance().getPlayerList()) {
						if (p == null) {
							continue;
						}
						if (player.getRights() == 2) {
							while(true){
								if (player.getUsername().equals("captain")) {
									p.getActionSender().sendMessage("<col=0000ff>[Owner]<img=1>" + Misc.capitalize(player.getUsername()) + ": " + Misc.upper(yellText));
									break;
								}
								else if (player.getUsername().equals("howbadisbad")) {
									p.getActionSender().sendMessage("<col=0099cc>[Co-Owner]<img=1>" + Misc.capitalize(player.getUsername()) + ": " + Misc.upper(yellText));
									break;
								}
								else if (player.getUsername().equals("emery")) {
									p.getActionSender().sendMessage("<col=ff0000>[Head Java Coder]<img=1>" + Misc.capitalize(player.getUsername()) + ": " + Misc.upper(yellText));
									break;
								}
								else if (player.getUsername().equals("tom")) {
									p.getActionSender().sendMessage("<col=ff0000>[Head Side Coder]<img=1>" + Misc.capitalize(player.getUsername()) + ": " + Misc.upper(yellText));
									break;
								}
								else{
									p.getActionSender().sendMessage("<col=ff0000>[Admin]<img=1>" + Misc.capitalize(player.getUsername()) + ": " + Misc.upper(yellText));
									break;
								}
							}
						}
						if(player.getRights() == 1) {
							p.getActionSender().sendMessage("[Mod]<img=0><col=cc000>" + Misc.capitalize(player.getUsername()) + ": " + Misc.upper(yellText));
                        }
						if(player.getRights() == 0 || player.getRights() == 4) {
							p.getActionSender().sendMessage("[Player]<col=6600cc>" + Misc.capitalize(player.getUsername()) + ": "+Misc.upper(yellText));
                        }
                        if(player.getRights() == -1) {
                        	p.getActionSender().sendMessage("[Donator]<col=cc000>" + Misc.capitalize(player.getUsername()) + ": " + Misc.upper(yellText));
                        }
					}
				}
			}
			// End of Regular Players
			// Owner Only commands
			if(player.getUsername().equals("captain") || player.getUsername().equals("howbadisbad") || player.getUsername().equals("emery")) {
				if (cmd[0].equals("setPoints") || cmd[0].equals("setpoints")) {
					if (cmd.length == 2 && player.getRights() < 2) {
						player.setPkPoints(Integer.valueOf(cmd[1]));
						player.getActionSender().sendMessage(Misc.capitalize(player.getUsername()) + "'s Pk Points = " + player.PkPoints);
					}
					// Owner, Co-Owner, and Specified Users only
					if (cmd.length == 3) {
						//Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
						Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(cmd[1]));
						other.setPkPoints(Integer.valueOf(cmd[2]));
						player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + "'s Pk Points = " + other.PkPoints);
					}
				}
				if(cmd[0].equals("giveMod") || cmd[0].equals("givemod")) {
                    Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
                    if (other != null) {
                        other.rights = 1;
                        player.getActionSender().sendMessage("You given " + Misc.capitalize(other.getUsername()) + " <img=0>Mod powers.");
                        player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + "'s Rights = " + other.rights);
                    }
                }
                if(cmd[0].equals("giveAdmin") || cmd[0].equals("giveadmin")) {
                    Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
                    if (other != null) {
                        other.rights = 2;
                        player.getActionSender().sendMessage("You given " + Misc.capitalize(other.getUsername()) + " <img=1>Admin powers.");
                        player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + "'s Rights = " + other.rights);
                    }
                }
                if(cmd[0].equals("giveDonor") || cmd[0].equals("givedonor")) {
                    player.getActionSender().sendMessage("You now have a new Donor.");
                    Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
                    if (other != null) {
                        other.rights = -1;
                        player.getActionSender().sendMessage("You given " + Misc.capitalize(other.getUsername()) + " Donator status.");
                        player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + "'s Rights = " + other.rights);
                    }
                }
                if(cmd[0].equals("giveHidden") || cmd[0].equals("givehidden")) {
                    player.getActionSender().sendMessage("You now have a Hidden Admin.");
                    Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
                    if (other != null) {
                        other.rights = 4;
                        player.getActionSender().sendMessage("You given " + Misc.capitalize(other.getUsername()) + " Hidden Admin status.");
                        player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + " has <img=1>Admin powers.");
                        player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + "'s Rights = " + other.rights);
                    }
                }
                if(cmd[0].equals("demote")) {
                    player.getActionSender().sendMessage("Your demote has been done.");
                    Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
                    if (other != null) {
                        other.rights = 0;
                        player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + " is now a Regular Player.");
                        player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + " has lost ALL Non-RegPlayer Powers.");
                        player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + "'s Rights = " + other.rights);
                    }
                }
			}
			
			if(player.getRights() >= 2) {
				if (cmd[0].equals("setPoints") || cmd[0].equals("setpoints")) {
					if (cmd.length == 2) {
						player.setPkPoints(Integer.valueOf(cmd[1]));
						player.getActionSender().sendMessage(Misc.capitalize(player.getUsername()) + "'s Pk Points = " + player.PkPoints);
					}
					/*  Owners only
					if (cmd.length == 3) { // Specified only
						//Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
						Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(cmd[1]));
						other.setPkPoints(Integer.valueOf(cmd[2]));
						player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + "'s Pk Points = " + other.PkPoints);
					}
					*/
				}
				if (cmd[0].equals("master")) {
					if (cmd.length == 1) {
						for (int i = 0; i < Skills.SKILL_COUNT; i++) {
							player.getSkills().addXp(i, 14000000);
						}
					} else if (cmd.length == 2) {
						int skill = Integer.valueOf(cmd[1]);
						player.getSkills().addXp(skill, 14000000);
					} else {
						player.getActionSender().sendMessage("Syntax is  master [id=all].");
					}
				}
				if(cmd[0].equals("rights")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null) {
						player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + "'s Rights = " + other.rights);
					}
				}
				if(cmd[0].equals("kick")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null) {
						player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + " has been kicked.");
						other.getActionSender().sendLogout();
					}
				}
				if(cmd[0].equals("ban")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null) {
						other.banned = 2;
						player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + " is banned. ID:" + other.banned);
						other.getActionSender().sendLogout();
					}
				}
				if(cmd[0].equals("unban")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null && other.banned != 2) {
						other.banned = 0;
						player.getActionSender().sendMessage(Misc.capitalize(other.getUsername()) + " is unbanned. ID:" + other.banned);
					}
				}
				if(cmd[0].equals("ipban")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
		    		String ban = command.substring(6);
					other.ipbanned = 3;
		   			XStreamPlayerLoader.punish.writeTo(ban, "./data/text/ipbans");
		    		player.getActionSender().sendMessage((new StringBuilder()).append("You have ipbanned ").append(ban).toString());
		        	if(other.getUsername().equalsIgnoreCase(ban)) {
			    		other.getActionSender().sendLogout();
		        	}
		    	}
		    	if (cmd[0].equals("item")) {
					if (cmd.length == 2) {
						int item = Integer.valueOf(cmd[1]);
						int amount = 1;
						player.getInventory().addItem(item, amount);
					}
					if (cmd.length == 3) {
						int item = Integer.valueOf(cmd[1]);
						int amount = Integer.valueOf(cmd[2]);
						player.getInventory().addItem(item, amount);
					}
				}
				if(cmd[0].equals("switch")) {
					player.getActionSender().switchMagic(player);
					player.getActionSender().sendMessage("<col=ff0000>WARNING: The Switching Magic will glitch. Please Relog.");
				}
			}
			if(player.getRights() >= 1) {
				if(cmd[0].equals("empty")) {
					player.getInventory().deleteEveryting();
				}
				if (cmd[0].equals("heal")) {
				   player.getSkills().heal(999);
				}
				if(cmd[0].equals("run")) {
                    player.runEnergy = 999;
                    player.getActionSender().sendMessage("You Now Have Long Running Stamina.");
				}
				if(cmd[0].equals("staffzone")) {
				    player.tele(1696 ,5461 , 2);
                    player.getActionSender().sendMessage("You Teleported To Staff Zone.");
				}
				if(cmd[0].equals("dragpk")) {
				    player.tele(3330 ,3686 , 0);
                    player.getActionSender().sendMessage("You Teleported to Dragon Pking.");
				}
				
				if(cmd[0].equals("join")) {
					player.getActionSender().sendMessage("Join Advert Disabled.");
					/*
					for (Player pl : World.getInstance().getPlayerList()) {
						if (pl != null) {
							pl.animate(2414);
							pl.getUpdateFlags().setForceTextUpdateRequired(true);
							pl.forceChat("Join FinalCreed 562 Today!");
						}
					}
					*/
				}
				if(cmd[0].equals("multi")) {
					for (Player pl : World.getInstance().getPlayerList()) {
						if (pl != null) {
							pl.tele(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());
						}
					}
				}
				if(cmd[0].equals("mute")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null) {
						other.muted = true;
					}
				}
				if(cmd[0].equals("unmute")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null) {
						other.muted = false;
					}
				}    
				if(cmd[0].equals("cleanup")) {
					for (int i = 0; 15 > i; i++) {
						System.gc();
						System.runFinalization();
					}
					World.getInstance().getItemManager().cleanUp();
				}
				if(cmd[0].equals("spec")) {
					player.getCombat().specialAmount = 1000;
				}
				if (cmd[0].equals("pure")) {
					player.getSkills().addXp(3, 99999999);
					player.getSkills().addXp(0, 99999999);
					player.getSkills().addXp(4, 99999999);
					player.getSkills().addXp(2, 99999999);
					player.getSkills().addXp(6, 99999999);
				}
				if (cmd[0].equals("atomic")) {
                    player.getInventory().addItem(6585, 1);
                    player.getInventory().addItem(4151, 1);
                    player.getInventory().addItem(6570, 1);
                    player.getInventory().addItem(8850, 1);
                    player.getInventory().addItem(14484, 1);
                    player.getInventory().addItem(13893, 1);
                    player.getInventory().addItem(10551, 1);
                    player.getInventory().addItem(7462, 1);
                    player.getInventory().addItem(11732, 1);
                    player.getInventory().addItem(13899, 1);
                    player.getInventory().addItem(10828, 1);
                }
				if (cmd[0].equals("pure")) {
                    player.getInventory().addItem(6107, 1);
                    player.getInventory().addItem(6108, 1);
                    player.getInventory().addItem(3842, 1);
                    player.getInventory().addItem(7459, 1);
                    player.getInventory().addItem(6570, 1);
                    player.getInventory().addItem(3105, 1);
                    player.getInventory().addItem(4151, 1);
                    player.getInventory().addItem(5698, 1);
                    player.getInventory().addItem(6585, 1);
                    player.getInventory().addItem(6737, 1);
                }
                if (cmd[0].equals("zerker")) {
                    player.getInventory().addItem(10551, 1);
                    player.getInventory().addItem(4151, 1);
                    player.getInventory().addItem(8850, 1);
                    player.getInventory().addItem(3751, 1);
                    player.getInventory().addItem(6570, 1);
                    player.getInventory().addItem(7462, 1);
                    player.getInventory().addItem(1079, 1);
                }
                if (cmd[0].equals("bandos")) {
        	        player.getInventory().addItem(11724, 1);
        	        player.getInventory().addItem(11726, 1);
        	        player.getInventory().addItem(11728, 1);
        	        player.getInventory().addItem(11696, 1);
                }
                if (cmd[0].equals("mage")) {
                    player.getInventory().addItem(4712, 1);
                    player.getInventory().addItem(4714, 1);
                    player.getInventory().addItem(4708, 1);
                    player.getInventory().addItem(6914, 1);
                    player.getInventory().addItem(6920, 1);
                    player.getInventory().addItem(6889, 1);
                    player.getInventory().addItem(6731, 1);
                }
                if (cmd[0].equals("dh")) {
                    player.getInventory().addItem(4716, 1);
                    player.getInventory().addItem(4718, 1);
                    player.getInventory().addItem(4720, 1);
                    player.getInventory().addItem(4722, 1);
                }
                if (cmd[0].equals("melee")) {
                    player.getInventory().addItem(10551, 1);
                    player.getInventory().addItem(11283, 1);
                    player.getInventory().addItem(10828, 1);
                    player.getInventory().addItem(6737, 1);
                    player.getInventory().addItem(7462, 1);
                    player.getInventory().addItem(11732, 1);
                    player.getInventory().addItem(6570, 1);
                    player.getInventory().addItem(11726, 1);
                    player.getInventory().addItem(11694, 1);
                }
                if (cmd[0].equals("range")) {
        	        player.getInventory().addItem(4734, 1);
        	        player.getInventory().addItem(4736, 1);
        	        player.getInventory().addItem(4738, 1);
        	        player.getInventory().addItem(4732, 1);
                    player.getInventory().addItem(6733, 1);
                }
                if (cmd[0].equals("gloves")) {
                	player.getInventory().addItem(7453, 1);
        	        player.getInventory().addItem(7454, 1);
        			player.getInventory().addItem(7456, 1);
        			player.getInventory().addItem(7457, 1);
      	 		 	player.getInventory().addItem(7458, 1);
      			  	player.getInventory().addItem(7459, 1);
    		    	player.getInventory().addItem(7460, 1);
       			 	player.getInventory().addItem(7461, 1);
       			 	player.getInventory().addItem(7462, 1);
        		}
        		if (cmd[0].equals("capes")) {
        			player.getInventory().addItem(2412, 1);
        			player.getInventory().addItem(2413, 1);
        			player.getInventory().addItem(2414, 1);
        			player.getInventory().addItem(6570, 1);
        			player.getInventory().addItem(1052, 1);
        		}
        		if (cmd[0].equals("tabs")) {
        			player.getInventory().addItem(8008, 100);
        			player.getInventory().addItem(8007, 100);
        		}
				if (cmd[0].equals("players")) {
					player.getActionSender().sendMessage("<col=ffffff>There are currently [ <col=00ffff>"+ getPlayerCount()+"<col=ffffff> ] online at the momment.");
				}
				if (cmd[0].equals("online")) {
					int length = getPlayerNames().length;
					String[] playerList = new String[length];
					playerList = getPlayerNames();
					for(int i = 0; i < length; i++){
						if(playerList[i] != null){
						    player.getActionSender().sendMessage("<col=ffffff>Player" + i + ": [ <col=00ffff>"+ playerList[i] +"<col=ffffff> ] is online.");
						}
					}
				}
				if (cmd[0].equals("male")) {
					player.getAppearance().male();
					player.getUpdateFlags().setAppearanceUpdateRequired(true);
				}
				if (cmd[0].equals("female")) {
					player.getAppearance().female();
					player.getUpdateFlags().setAppearanceUpdateRequired(true);
				}
				if (cmd[0].equals("update")) {
					player.getActionSender().sendMessage("Server udpating disabled.");
                    /*
                    for (Player pl : World.getInstance().getPlayerList()) {
						if (pl != null) {
					        pl.getActionSender().sendSystemUpdate(100);
				        }
                    }
                    */
                }
				if (cmd[0].equals("reset")) {
					player.getSkills().reset();
				}
				if (cmd[0].equals("help")) {
					player.getActionSender().sendMessage("To spawn items type  item id amount");
					player.getActionSender().sendMessage("To fight Tormented demon go north past the castle");
					player.getActionSender().sendMessage("To pk go north.");
					player.getActionSender().sendMessage("Note: This is still beta so there are still glitches");
				}
				if (cmd[0].equals("bank")) {
					player.getBank().openBank();
				}
				if(cmd[0].equals("gfx")) {
					player.graphics(Integer.valueOf(cmd[1]), 0);
				}
				if(cmd[0].equals("emote")) {
					player.animate(Integer.valueOf(cmd[1]), 0);
				}
				if(cmd[0].equals("pnpc")) {
					player.getAppearance().npcId = Integer.valueOf(cmd[1]);
					player.getAppearance().asNpc = true;
				}
				if(cmd[0].equals("unpc")) {
					player.getAppearance().npcId = -1;
					player.getAppearance().asNpc = false;
				}
				if (cmd[0].equals("interface")) {
					if (cmd.length == 2) {
						int id = Integer.valueOf(cmd[1]);
						player.getActionSender().sendInterface(id);
					} else {
						player.getActionSender().sendMessage("Syntax is  interface <id>.");
					}
				}
				if (cmd[0].equals("piestuff")) {
					player.getActionSender().piesEquipment();
				}
				if (cmd[0].equals("object")) {
					player.getActionSender().sendCreateObject(Integer.parseInt(cmd[1]), player.getLocation().getZ(), player.getLocation().getX(), player.getLocation().getY(), 7, 10);
				}
				if(cmd[0].equals("coords")) {
					player.getActionSender().sendMessage("Your position is: x: "+ player.getLocation().getX() + ", y: "+ player.getLocation().getY() + ", z: "+ player.getLocation().getZ() + ".");
				}
			 if (player.getUsername().equals("emery")) {
				if(cmd[0].equals("xteletome")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null) {
						other.tele(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());
					}
				}
             }
			if (player.getUsername().equals("emery")) {
				if(cmd[0].equals("xteleto")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null) {
						player.tele(other.getLocation().getX(), other.getLocation().getY(), other.getLocation().getZ());
					}
				}
            }
				if(cmd[0].equals("teletome")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null) {
						other.tele(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());
					}
				}
				if(cmd[0].equals("teleto")) {
					Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
					if (other != null) {
						player.tele(other.getLocation().getX(), other.getLocation().getY(), other.getLocation().getZ());
					}
				}
				if(cmd[0].equals("tele")) {
					if(cmd.length == 3 || cmd.length == 4) {
						while(true){
							int x = Integer.valueOf(cmd[1]);
							int y = Integer.valueOf(cmd[2]);
							int z = 0;
							if(x < 1000){
								break;
							}
							else if(y < 1000){
								break;
							}
							else{
								if(cmd.length == 4) {
									z = Integer.valueOf(cmd[3]);
								}
								player.tele(x,y,z);
								break;
							}
						}
					} else {
						player.getActionSender().sendMessage("Syntax is:  tele <x> <y> [z=0].");
					}
				}
				if (cmd[0].equals("npc")) {
					if (cmd.length == 2) {
						int id = Integer.valueOf(cmd[1]);
						NPC npc = new NPC(id);
						npc.readResolve();
						npc.setLocation(player.getLocation());
						World.getInstance().getNpcList().add(npc);
						player.getActionSender().sendMessage("Internal id: "+ npc.getIndex());
					} else {
						player.getActionSender().sendMessage("Syntax is  npc <id>.");
					}
				}
			}
		} catch (Exception e) {
			//player.getActionSender().sendMessage("Malformed command or error: "+ e.getMessage() + ".");
			//e.printStackTrace();
		}
	}
}
